﻿<!DOCTYPE html>
<html>
<head>
	<link href="../resources/demos.css" rel="stylesheet" type="text/css" />

	<link href="../resources/movielist.css" rel="stylesheet" type="text/css" />
	<link href="../resources/syntaxhighlighter.css" rel="stylesheet" type="text/css" />
	<script src="../resources/syntaxhighlighter.min.js" type="text/javascript"></script>

	<script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
	<script src="../../jsrender.js" type="text/javascript"></script>
</head>
<body>
<div class="nav"><a href="13_converters.html">Prev</a> <a href="index.html">Index</a></div>

<h3>JsRender: Custom tags for rendering</h3>

<script id="movieTemplate" type="text/x-jsrender">
	<tr>
		<td>{{>title}}</td>
		<td>{{sort languages tmpl="#sortedTemplate"/}}</td>
		<td>
			{{sort languages reverse=true}}
				<div>
					<b>{{>name}}</b>
				</div>
			{{/sort}}
		</td>
	</tr>
</script>

<script id="sortedTemplate" type="text/x-jsrender">
	<div>{{>name}}</div>
</script>

<table>
	<thead><tr><th>Title</th><th>Orginal order</th><th>Reverse order</th></tr></thead>
	<tbody id="movieList"></tbody>
</table>
<div id="cons"></div>
<script type="text/javascript">

	$.views.tags({

		sort: function( array ){
			var ret = "";
			if ( this.tagCtx.props.reverse ) {
				// Render in reverse order
				for ( var i = array.length; i; i-- ) {
					ret += this.tagCtx.render( array[ i - 1 ] );
				}
			} else {
				// Render in original order
				ret += this.tagCtx.render( array );
			}
			return ret;
		}

	});

	var movies = [
		{
			title: "Meet Joe Black",
			languages: [
				{ name: "English" },
				{ name: "French" }
			]
		},
		{
			title: "Eyes Wide Shut",
			languages: [
				{ name: "French" },
				{ name: "German" },
				{ name: "Spanish" }
			]
		}
	];

	$( "#movieList" ).html(
		$( "#movieTemplate" ).render( movies )
	);
</script>

<!--================ End of Demo Section ================-->

<h4>HTML:</h4>
<pre class="brush: xml;">
&lt;td>
    {{sort languages reverse="true"}}
        &lt;div>{{>name}}&lt;/div>
    {{/sort}}
&lt;/td>
</pre>

<h4>Script:</h4>
<pre class="brush: js;">
$.views.tags({

    sort: function( array ){
        ...
        if ( this.tagCtx.props.reverse ) {
            // Render in reverse order
            for ( var l = array.length, i = l; i; i-- ) {
                ret += $.render( array[ i - 1 ], this.tmpl );
            }
        }
        ...
        return ret;
    }

});
</pre>

</body>
</html>

